package controllers;

import beans.Contract;
import beans.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import services.impl.ContractServicesImpl;
import services.impl.ProductServicesImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractServicesImpl csi;

    @RequestMapping("/contract")
    public List contract(){
        List<Contract> list = csi.selectList();
        return list;
    }

    @RequestMapping("/createcontract")
    public String createContract(@RequestBody Contract contract){
        csi.insertSelective(contract);
        return null;
    }

    @RequestMapping("/editcontract")
    public String editContract(@RequestBody Contract contract){
        csi.updateByPrimaryKeySelective(contract);
        return null;
    }

    @RequestMapping("/detailscontract")
    public Contract detailsContract(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("contractId"));
        Contract contract = csi.selectByPrimaryKey(i);
        return contract;
    }

    @RequestMapping("/searchcontract")
    public List<Contract> searchContract(@RequestBody Map<String,String> request){
        String select = request.get("selectText");
        String input = request.get("inputText");
        List<Contract> list = csi.selectSelective(select,input);
        return list;
    }

    @RequestMapping("/deletecontract")
    public String deleteContract(HttpServletRequest request) {
        int i = Integer.parseInt(request.getParameter("contractId"));
        csi.deleteByPrimaryKey(i);
        return null;
    }
}
